/*
 * verifysignpkg.h
 *
 *  Created on: 2011-12-28
 *      Author: Steven
 */

#include <stdio.h>
#include <jni.h>

#ifndef VERIFYSIGNPKG_H_
#define VERIFYSIGNPKG_H_

const char* activityName = "com/asda/asdad";
const char* correctPackageNameMD5 = "4063ab5eb0051cb00d1efa837f5b3a02";
const char* correctSignatureMD5 = "0b26b31e1542305fa04ea7a4599ede78";
	
jstring getPackageName(JNIEnv *env, jobject ctx) {
	jclass clazzEntryActivity = NULL;
 	jfieldID fieldIDstaticContext = NULL;
 	jmethodID methodIDgetPackageName = NULL;

 	jmethodID methodIDgetPackageManager = NULL;
 	jclass clazzPackageManager = NULL;
 	jmethodID methodIDgetPackageInfo = NULL;
 	jfieldID fieldIDstaticSignatureFlag = NULL;
 	int signatureFlag = 0;

 	jclass clazzPackageInfo = NULL;
 	jfieldID fieldIDSignatures = NULL;

 	jclass clazzSignature = NULL;
 	jmethodID methodIDSignatureToCharsString = NULL;
 	
	clazzEntryActivity = (*env)->FindClass(env, activityName);
	
	//fieldIDstaticContext = (*env)->GetStaticFieldID(env, clazzEntryActivity, activityField, "Landroid/app/Activity;");
	
	methodIDgetPackageName = (*env)->GetMethodID(env, clazzEntryActivity, "getPackageName", "()Ljava/lang/String;");
	
	methodIDgetPackageManager = (*env)->GetMethodID(env, clazzEntryActivity, "getPackageManager", "()Landroid/content/pm/PackageManager;");
	
	clazzPackageManager = (*env)->FindClass(env, "android/content/pm/PackageManager");
	
	methodIDgetPackageInfo = (*env)->GetMethodID(env, clazzPackageManager, "getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
	
	fieldIDstaticSignatureFlag = (*env)->GetStaticFieldID(env, clazzPackageManager, "GET_SIGNATURES", "I");
	
	signatureFlag = (*env)->GetStaticIntField(env, clazzPackageManager, fieldIDstaticSignatureFlag);
	
	clazzPackageInfo = (*env)->FindClass(env, "android/content/pm/PackageInfo");
	
	fieldIDSignatures = (*env)->GetFieldID(env, clazzPackageInfo, "signatures" , "[Landroid/content/pm/Signature;");
	
	clazzSignature = (*env)->FindClass(env, "android/content/pm/Signature");
	
	methodIDSignatureToCharsString = (*env)->GetMethodID(env, clazzSignature, "toCharsString", "()Ljava/lang/String;");
	
	jobject objectContext = ctx;
	
	if ( objectContext == NULL || methodIDgetPackageName == NULL ) {
		__android_log_write(ANDROID_LOG_DEBUG,"tag","objectContext == NULL or methodIDgetPackageName == NULL");
		char s[15] = "something null";
		jstring nullPackageName = (*env)->NewStringUTF(env, s);
		return nullPackageName;
	} else {
		jstring jPackageName = (jstring)(*env)->CallObjectMethod(env, objectContext, methodIDgetPackageName);
		return jPackageName;
	}
	
}



jstring getSignatureMd5(JNIEnv *env, jobject ctx) {
	jclass clazzEntryActivity = NULL;
 	jfieldID fieldIDstaticContext = NULL;
 	jmethodID methodIDgetPackageName = NULL;

 	jmethodID methodIDgetPackageManager = NULL;
 	jclass clazzPackageManager = NULL;
 	jmethodID methodIDgetPackageInfo = NULL;
 	jfieldID fieldIDstaticSignatureFlag = NULL;
 	int signatureFlag = 0;

 	jclass clazzPackageInfo = NULL;
 	jfieldID fieldIDSignatures = NULL;

 	jclass clazzSignature = NULL;
 	jmethodID methodIDSignatureToCharsString = NULL;
 	
	clazzEntryActivity = (*env)->FindClass(env, activityName);
	
	//fieldIDstaticContext = (*env)->GetStaticFieldID(env, clazzEntryActivity, activityField, "Landroid/app/Activity;");
	
	methodIDgetPackageName = (*env)->GetMethodID(env, clazzEntryActivity, "getPackageName", "()Ljava/lang/String;");
	
	methodIDgetPackageManager = (*env)->GetMethodID(env, clazzEntryActivity, "getPackageManager", "()Landroid/content/pm/PackageManager;");
	
	clazzPackageManager = (*env)->FindClass(env, "android/content/pm/PackageManager");
	
	methodIDgetPackageInfo = (*env)->GetMethodID(env, clazzPackageManager, "getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
	
	fieldIDstaticSignatureFlag = (*env)->GetStaticFieldID(env, clazzPackageManager, "GET_SIGNATURES", "I");
	
	signatureFlag = (*env)->GetStaticIntField(env, clazzPackageManager, fieldIDstaticSignatureFlag);
	
	clazzPackageInfo = (*env)->FindClass(env, "android/content/pm/PackageInfo");
	
	fieldIDSignatures = (*env)->GetFieldID(env, clazzPackageInfo, "signatures" , "[Landroid/content/pm/Signature;");
	
	clazzSignature = (*env)->FindClass(env, "android/content/pm/Signature");
	
	methodIDSignatureToCharsString = (*env)->GetMethodID(env, clazzSignature, "toCharsString", "()Ljava/lang/String;");
	
	
	
	
	jobject objectContext = ctx;
	jstring jPackageName = (jstring)(*env)->CallObjectMethod(env, objectContext, methodIDgetPackageName);
	
	jobject object_pm = (*env)->CallObjectMethod(env, objectContext, methodIDgetPackageManager);
	jobject object_pi = (*env)->CallObjectMethod(env, object_pm, methodIDgetPackageInfo, jPackageName, signatureFlag);
	
	jobjectArray signatures = (jobjectArray)(*env)->GetObjectField(env, object_pi, fieldIDSignatures);
	jobject firstSignature = (*env)->GetObjectArrayElement(env, signatures, 0);
	
	jstring jSignatureChars = (jstring)(*env)->CallObjectMethod(env, firstSignature, methodIDSignatureToCharsString);
	
	char* toVerifySignature = (char*)(*env)->GetStringUTFChars(env, jSignatureChars, 0);
	
	char* toVerifySignatureMD5 = MDString(toVerifySignature);
	
	return (*env)->NewStringUTF(env, toVerifySignatureMD5);
	
}



	
int verifyPackageName(JNIEnv *env, jobject ctx) {

 	jclass clazzEntryActivity = NULL;
 	jmethodID methodIDgetPackageName = NULL;

 	jclass clazzSignature = NULL;
 	jmethodID methodIDSignatureToCharsString = NULL;
 	
	clazzEntryActivity = (*env)->FindClass(env, "android/content/ContextWrapper");
	
	methodIDgetPackageName = (*env)->GetMethodID(env, clazzEntryActivity, "getPackageName", "()Ljava/lang/String;");

	jobject objectContext = ctx;
	
	if ( objectContext == NULL || methodIDgetPackageName == NULL ) {
		//__android_log_write(ANDROID_LOG_DEBUG,"tag","objectContext == NULL or methodIDgetPackageName == NULL");
	}
	jstring jPackageName = (jstring)(*env)->CallObjectMethod(env, objectContext, methodIDgetPackageName);
	
	char* toVerifyPackageName = (char*)(*env)->GetStringUTFChars(env, jPackageName, 0);
	//__android_log_write(ANDROID_LOG_DEBUG,"pmxxxxx",toVerifyPackageName);
	
	char* toVerifyPackageNameMD5 = MDString(toVerifyPackageName);
	//__android_log_write(ANDROID_LOG_DEBUG,"pmxxxxx",toVerifyPackageNameMD5);
	//(*env)->ReleaseStringUTFChars(env, jPackageName, toVerifyPackageName);

	return strcmp(toVerifyPackageNameMD5, correctPackageNameMD5);
	
}


int verifySignature(JNIEnv *env, jobject ctx) {

	jclass clazzEntryActivity = NULL;
 	jmethodID methodIDgetPackageName = NULL;

 	jmethodID methodIDgetPackageManager = NULL;
 	jclass clazzPackageManager = NULL;
 	jmethodID methodIDgetPackageInfo = NULL;
 	jfieldID fieldIDstaticSignatureFlag = NULL;
 	int signatureFlag = 0;

 	jclass clazzPackageInfo = NULL;
 	jfieldID fieldIDSignatures = NULL;

 	jclass clazzSignature = NULL;
 	jmethodID methodIDSignatureToCharsString = NULL;
 	
	clazzEntryActivity = (*env)->FindClass(env, "android/content/ContextWrapper");
	
	methodIDgetPackageName = (*env)->GetMethodID(env, clazzEntryActivity, "getPackageName", "()Ljava/lang/String;");
	
	methodIDgetPackageManager = (*env)->GetMethodID(env, clazzEntryActivity, "getPackageManager", "()Landroid/content/pm/PackageManager;");
	
	clazzPackageManager = (*env)->FindClass(env, "android/content/pm/PackageManager");
	
	methodIDgetPackageInfo = (*env)->GetMethodID(env, clazzPackageManager, "getPackageInfo", "(Ljava/lang/String;I)Landroid/content/pm/PackageInfo;");
	
	fieldIDstaticSignatureFlag = (*env)->GetStaticFieldID(env, clazzPackageManager, "GET_SIGNATURES", "I");
	
	signatureFlag = (*env)->GetStaticIntField(env, clazzPackageManager, fieldIDstaticSignatureFlag);
	
	clazzPackageInfo = (*env)->FindClass(env, "android/content/pm/PackageInfo");
	
	fieldIDSignatures = (*env)->GetFieldID(env, clazzPackageInfo, "signatures" , "[Landroid/content/pm/Signature;");
	
	clazzSignature = (*env)->FindClass(env, "android/content/pm/Signature");
	
	methodIDSignatureToCharsString = (*env)->GetMethodID(env, clazzSignature, "toCharsString", "()Ljava/lang/String;");
	
	
	
	jobject objectContext = ctx;
	jstring jPackageName = (jstring)(*env)->CallObjectMethod(env, objectContext, methodIDgetPackageName);
	
	jobject object_pm = (*env)->CallObjectMethod(env, objectContext, methodIDgetPackageManager);
	jobject object_pi = (*env)->CallObjectMethod(env, object_pm, methodIDgetPackageInfo, jPackageName, signatureFlag);
	
	jobjectArray signatures = (jobjectArray)(*env)->GetObjectField(env, object_pi, fieldIDSignatures);
	jobject firstSignature = (*env)->GetObjectArrayElement(env, signatures, 0);
	
	jstring jSignatureChars = (jstring)(*env)->CallObjectMethod(env, firstSignature, methodIDSignatureToCharsString);
	
	char* toVerifySignature = (char*)(*env)->GetStringUTFChars(env, jSignatureChars, 0);
	//__android_log_write(ANDROID_LOG_DEBUG,"sign",toVerifySignature);
	
	char* toVerifySignatureMD5 = MDString(toVerifySignature);
	//__android_log_write(ANDROID_LOG_DEBUG,"signmd5",toVerifySignatureMD5);
	
	//(*env)->ReleaseStringUTFChars(env, jSignatureChars, toVerifySignature);
	
	return strcmp(toVerifySignatureMD5, correctSignatureMD5);
	
}


#endif /* VERIFYSIGNPKG_H_ */
